package com.itheima.protocol.distributedLock;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.util.concurrent.CountDownLatch;

public class ZookeeperClient {

    private final static String CONNECTIONSTRING = "192.168.1.108:2181";
    private final static int SESSIONTIMEOUT = 5000;


    //获取连接
    public static ZooKeeper getInstance() throws Exception {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zooKeeper = new ZooKeeper(CONNECTIONSTRING, SESSIONTIMEOUT, new Watcher() {

            public void process(WatchedEvent event) {
                if (event.getState() == Event.KeeperState.SyncConnected) {
                    countDownLatch.countDown();
                    System.out.println(event.getState());
                }
            }
        });

        countDownLatch.await();
        return zooKeeper;
    }

    public static int getSESSIONTIMEOUT() {
        return SESSIONTIMEOUT;
    }
}
